package com.weilele.leisure.time.ui.module.video_detail


import androidx.lifecycle.LifecycleOwner
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.bean.db.CommonDb
import com.weilele.leisure.time.ui.player.VideoModel
import com.weilele.leisure.time.ui.view.video.PlayerVideoView
import com.weilele.mvvm.app
import com.weilele.mvvm.base.helper.ILifecycleObserver
import com.weilele.mvvm.utils.safeGet
import com.weilele.leisure.time.bean.db.HistoryVideoBean
import com.weilele.mvvm.utils.coroutine.switchToThread
import com.yizisu.playerlibrary.IYzsPlayer
import com.yizisu.playerlibrary.helper.SimplePlayerListener
import kotlinx.coroutines.CoroutineScope

class VideoDetailPlayerHelper : ILifecycleObserver, SimplePlayerListener<VideoModel> {
    companion object {

        fun toHistoryVideoBean(
            video: VideoDetailBean?,
            listBean: VideoDetailBean.PlayersDTO?,
            dto: VideoDetailBean.PlayersDTO.DatasDTO?,
            currentDuration: Long, totalDuration: Long
        ): HistoryVideoBean {
            val bean = HistoryVideoBean()
            video ?: return bean
            listBean ?: return bean
            dto ?: return bean
            bean.copyData(video)
            bean.currentDuration = currentDuration
            bean.totalDuration = totalDuration
            bean.playTitle = dto.text
            bean.playUrl = dto.playUrl
            bean.listName = listBean.name
            bean.listKey = listBean.key
            return bean
        }
    }

    val player = IYzsPlayer.invoke<VideoModel>(app)

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        owner.lifecycle.addObserver(player)
        player.addPlayerListener(this)
        player.setAudioForceEnable(true)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        owner.lifecycle.removeObserver(player)
    }

    fun setPlayerVideoView(videoView: PlayerVideoView?) {
        videoView?.setPlayerAndInitHelper(player.safeGet()!!) {
            it.updateFullScreenChange(false)
        }
    }

    override suspend fun onTick(playerModel: VideoModel, scope: CoroutineScope) {
        super.onTick(playerModel, scope)
        switchToThread {
            saveToDb(playerModel)
        }
    }

    override fun onPlayerModelChange(lastModel: VideoModel?, playerModel: VideoModel) {
        super.onPlayerModelChange(lastModel, playerModel)
        saveToDb(lastModel)
        saveToDb(playerModel)
    }

    /**
     * 开始播放
     */
    fun startPlay(
        video: VideoDetailBean?,
        playerList: VideoDetailBean.PlayersDTO?,
        urls: MutableList<VideoDetailBean.PlayersDTO.DatasDTO>?,
        position: Int = 0,
        progress: Long = 0,
    ) {
        video ?: return
        playerList ?: return
        if (urls.isNullOrEmpty()) {
            return
        }
        player.prepare(urls.map {
            VideoModel(video, playerList, it)
        }.toMutableList(), position, true, progress)
    }


    /**
     * 数据保存到数据库
     */
    private fun saveToDb(playerModel: VideoModel?) {
        val video = playerModel?.video ?: return
        CommonDb.savePlayVideo(
            toHistoryVideoBean(
                video,
                playerModel.listBean,
                playerModel.dto,
                playerModel.currentDuration,
                playerModel.totalDuration
            )
        )
    }

}